package com.code.bolg.controller;

import com.code.bolg.bean.Article;
import com.code.bolg.output.ArticleOutPut;
import com.code.bolg.repository.ArticleRepository;
import com.code.bolg.service.ArticleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * article
 *
 * @author  2018-10-12
 */
//@RestController
//@RequestMapping("/article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 新增或编辑
     */
    @PostMapping("/saveOrUpdate")
    public Object save(Article article) {
        if(article.getArticleId()!=null){
            article.setUpdateDate(new Date());
        }else if(article.getArticleId()==null){
            article.setCreateDate(new Date());
            article.setUpdateDate(article.getCreateDate());
        }
        Article save = articleService.save(article);
        ArticleOutPut outPut = new ArticleOutPut();
        BeanUtils.copyProperties(save,outPut);
        return outPut;
    }

    /**
     * 删除
     */
    @GetMapping("/delete")
    public Object delete(int id) {
        return articleService.deleteById(id);
    }

    /**
     * 查询
     */
    @GetMapping("/find")
    public Object find(int id) {
        Article article = articleService.findById(id);
        return article;
    }

    /**
     * 分页查询
     */
    @PostMapping("/list")
    public Object list(Article article,
                       @RequestParam(required = false, defaultValue = "0") int page,
                       @RequestParam(required = false, defaultValue = "10") int size) {
        List<Article> list = articleService.findAll(article, page-1==-1?0:page, size);
        return list;
    }

}

